<template>
  <div id="traffic">
    <nav-bar :title="title" :isShow="false"></nav-bar>
    <van-form @submit="onSubmit">
      <van-collapse v-model="activeNames">
        <van-collapse-item title="基础信息" name="1">
          <van-field
            name="traffic_time"
            label="事故时间"
            placeholder="请事故时间"
            readonly
            clickable
            :value="form.traffic_time"
            @click="onDateClick('traffic_time')"
          />
          <van-field
            v-model="form.age"
            name="age"
            label="年  龄"
            placeholder="请填写年龄"
          />
          <van-field
            readonly
            clickable
            name="address"
            :value="form.address"
            label="家庭地址"
            placeholder="住所地或常住地"
            @click="onAreaClick('address')"
          />
          <van-field
            readonly
            clickable
            :value="form.court_addr"
            name="court_addr"
            label="法院地址"
            placeholder="受理法院所在地"
            @click="onAreaClick('court_address')"
          />
          <van-field name="dead" label="是否死亡">
            <template #input>
              <van-radio-group
                v-model="form.dead"
                direction="horizontal"
                @change="onRadioChange"
              >
                <van-radio name="1" @click="onDeadClick('1')">是</van-radio>
                <van-radio name="0" @click="onDeadClick('0')">否</van-radio>
              </van-radio-group>
            </template>
          </van-field>
          <van-field
            v-model="form.funeral_expense"
            name="funeral_expense"
            type="number"
            label="丧葬费"
            v-if="form.dead == '1'"
            placeholder="丧葬费"
          >
            <template #button>
              <van-button plain size="small" type="default">元</van-button>
            </template>
          </van-field>
          <van-field name="fix_income" label="固定收入">
            <template #input>
              <van-radio-group v-model="form.fix_income" direction="horizontal">
                <van-radio name="1">有</van-radio>
                <van-radio name="0">无</van-radio>
              </van-radio-group>
            </template>
          </van-field>
          <van-field
            v-model="form.month_income_money"
            name="month_income_money"
            type="number"
            label="月收入"
            v-if="form.fix_income == 1"
            placeholder="月收入金额"
          >
            <template #button>
              <van-button plain size="small" type="default">元</van-button>
            </template>
          </van-field>
          <van-field
            v-model="form.prove_income"
            name="prove_income"
            type="number"
            label="举证月收入"
            v-if="form.fix_income == 0"
            placeholder="举证三年收入的平均月收入"
          >
            <template #button>
              <van-button plain size="small" type="default">元</van-button>
            </template>
          </van-field>
          <van-field
            readonly
            clickable
            :value="form.industry"
            name="industry"
            label="所属行业"
            placeholder="从事的行业"
            v-if="form.fix_income == 0"
            @click="showIndustryPicker = true"
          />
          <van-field
            v-model="form.industry_wage"
            name="industry_wage"
            type="number"
            label="行业收入"
            placeholder="行业收入"
            readonly
            v-if="form.fix_income == 0"
          >
            <template #button>
              <van-button plain size="small" type="default">元</van-button>
            </template>
          </van-field>
          <van-field
            v-model="form.spirit_fee"
            name="spirit_fee"
            type="number"
            label="精神抚慰金"
            placeholder="精神抚慰金"
          >
            <template #button>
              <van-button plain size="small" type="default">元</van-button>
            </template>
          </van-field>
          <van-cell-group title="其他收入">
            <div v-for="(item, idx) in form.other_income" :key="'other' + idx">
              <van-field
                v-model="item.name"
                :label="'收入名称[' + (idx + 1) + ']'"
                placeholder="收入名称"
              >
                <template #button>
                  <van-button
                    type="danger"
                    icon="delete"
                    size="mini"
                    @click.stop.prevent="onHandleDelIncome(idx)"
                  ></van-button>
                </template>
              </van-field>
              <van-field
                v-model="item.money"
                label="收入金额"
                type="number"
                placeholder="收入金额"
              >
                <template #button>
                  <van-button plain size="small" type="default">元</van-button>
                </template>
              </van-field>
            </div>
          </van-cell-group>
          <van-row style="margin-top: 5px">
            <van-col span="8" offset="1">
              <van-button
                style="padding: 3px"
                type="primary"
                size="mini"
                @click.prevent="onHandleAddIncome"
                >增加其他收入项</van-button
              >
            </van-col>
          </van-row>
          <van-field
            v-model="form.other_fee"
            name="other_fee"
            type="number"
            label="其他费用"
            placeholder="其他费用"
          >
            <template #button>
              <van-button plain size="small" type="default">元</van-button>
            </template>
          </van-field>
          <van-field
            v-model="form.asset_lost"
            name="asset_lost"
            type="number"
            label="财产损失"
            placeholder="财产损失"
          >
            <template #button>
              <van-button plain size="small" type="default">元</van-button>
            </template>
          </van-field>
          <van-field
            readonly
            clickable
            name="duty_confirm"
            :value="form.duty_confirm"
            label="责任认定"
            placeholder="点击责任认定"
            @click="showPicker = true"
          />
        </van-collapse-item>

        <!-- 住院信息 -->
        <van-collapse-item title="住院信息" name="2">
          <van-field
            readonly
            clickable
            :value="form.enter_time"
            name="enter_time"
            label="入院日期"
            placeholder="请填写入院日期"
            @click="onDateClick('enter_time')"
          />
          <!-- :rules="[{ required: true, message: '请填写入院日期' }]" -->
          <van-field
            readonly
            clickable
            :value="form.leave_time"
            name="leave_time"
            label="出院日期"
            placeholder="请填写出院日期"
            @click="onDateClick('leave_time')"
          />
          <van-field
            v-model="form.hospital_day"
            name="hospital_day"
            label="住院天数"
            placeholder="住院天数"
            readonly
          >
            <template #button>
              <van-button plain size="small" type="default">天</van-button>
            </template>
          </van-field>
          <van-field
            v-model="form.hospital_costs"
            name="hospital_costs"
            type="number"
            label="医药治疗费"
            placeholder="医药费、治疗费"
          >
            <template #button>
              <van-button plain size="small" type="default">元</van-button>
            </template>
          </van-field>
          <van-field
            v-model="form.rest_day"
            name="rest_day"
            type="number"
            label="休养天数"
            placeholder="医嘱中需要休养天数"
          >
            <template #button>
              <van-button plain size="small" type="default">天</van-button>
            </template>
          </van-field>
          <van-field name="nutrition" label="医嘱加强营养">
            <template #input>
              <van-radio-group v-model="form.nutrition" direction="horizontal">
                <van-radio name="1">是</van-radio>
                <van-radio name="0">否</van-radio>
              </van-radio-group>
            </template>
          </van-field>
          <van-field
            v-model="form.leave_nutrition_fee"
            name="leave_nutrition_fee"
            type="number"
            label="出院后营养费"
            placeholder="以出院医嘱为准"
          >
            <template #button>
              <van-button plain size="small" type="default">元</van-button>
            </template>
          </van-field>
          <van-field
            v-model="form.device_cost"
            name="device_cost"
            type="number"
            label="医疗辅助器具"
            placeholder="以发票为准"
          >
            <template #button>
              <van-button plain size="small" type="default">元</van-button>
            </template>
          </van-field>
          <van-field
            v-model="form.car_fee"
            name="car_fee"
            type="number"
            label="交通费"
            placeholder="以发票为准"
          >
            <template #button>
              <van-button plain size="small" type="default">元</van-button>
            </template>
          </van-field>
          <van-field
            v-model="form.hotel_fee"
            name="hotel_fee"
            type="number"
            label="住宿费"
            placeholder="以发票为准"
          >
            <template #button>
              <van-button plain size="small" type="default">元</van-button>
            </template>
          </van-field>
          <van-field
            v-model="form.cosmetic_fee"
            name="cosmetic_fee"
            type="number"
            label="整容费"
            placeholder="以发票为准"
          >
            <template #button>
              <van-button plain size="small" type="default">元</van-button>
            </template>
          </van-field>
          <van-collapse-item title="一级护理" name="21">
            <van-tabs v-model="activeTab" animated>
              <van-tab title="护理人1" name="n1">
                <van-field name="nurse_one" label="工作状况">
                  <template #input>
                    <van-radio-group
                      v-model="form.nurse_one"
                      direction="horizontal"
                      @change="onNurseOneChange"
                    >
                      <van-radio name="1" @click="onNurseOneClick('1')"
                        >有工作</van-radio
                      >
                      <van-radio name="0" @click="onNurseOneClick('0')"
                        >无工作</van-radio
                      >
                    </van-radio-group>
                  </template>
                </van-field>
                <van-field
                  v-model="form.nurse_one_wage"
                  name="nurse_one_wage"
                  type="number"
                  label="日工资"
                  placeholder="需工作证明"
                >
                  <template #button>
                    <van-button plain size="small" type="default"
                      >元</van-button
                    >
                  </template>
                </van-field>
                <van-field
                  v-model="form.nurse_one_days"
                  name="nurse_one_days"
                  type="number"
                  label="护理天数"
                  placeholder="护理天数"
                >
                  <template #button>
                    <van-button plain size="small" type="default"
                      >天</van-button
                    >
                  </template>
                </van-field>
              </van-tab>
              <van-tab title="护理人2" name="n2">
                <van-field name="nurse_two" label="工作状况">
                  <template #input>
                    <van-radio-group
                      v-model="form.nurse_two"
                      direction="horizontal"
                      @change="onNurseTwoChange"
                    >
                      <van-radio name="1" @click="onNurseTwoClick('1')"
                        >有工作</van-radio
                      >
                      <van-radio name="0" @click="onNurseTwoClick('0')"
                        >无工作</van-radio
                      >
                    </van-radio-group>
                  </template>
                </van-field>
                <van-field
                  v-model="form.nurse_two_wage"
                  name="nurse_two_wage"
                  type="number"
                  label="日工资"
                  placeholder="需工作证明"
                >
                  <template #button>
                    <van-button plain size="small" type="default"
                      >元</van-button
                    >
                  </template>
                </van-field>
                <van-field
                  v-model="form.nurse_two_days"
                  name="nurse_two_days"
                  type="number"
                  label="护理天数"
                  placeholder="护理天数"
                >
                  <template #button>
                    <van-button plain size="small" type="default"
                      >天</van-button
                    >
                  </template>
                </van-field>
              </van-tab>
            </van-tabs>
          </van-collapse-item>
          <van-collapse-item title="二级护理" name="22">
            <van-field name="nurse_three" label="工作状况">
              <template #input>
                <van-radio-group
                  v-model="form.nurse_three"
                  direction="horizontal"
                  @change="onNurseThreeChange"
                >
                  <van-radio name="1" @click="onNurseThreeClick('1')"
                    >有工作</van-radio
                  >
                  <van-radio name="0" @click="onNurseThreeClick('0')"
                    >无工作</van-radio
                  >
                </van-radio-group>
              </template>
            </van-field>
            <van-field
              v-model="form.nurse_three_wage"
              name="nurse_three_wage"
              type="number"
              label="日工资"
              placeholder="需工作证明"
            >
              <template #button>
                <van-button plain size="small" type="default">元</van-button>
              </template>
            </van-field>
            <van-field
              v-model="form.nurse_three_days"
              name="nurse_three_days"
              type="number"
              label="护理天数"
              placeholder="护理天数"
            >
              <template #button>
                <van-button plain size="small" type="default">天</van-button>
              </template>
            </van-field>
          </van-collapse-item>
        </van-collapse-item>
        <van-collapse-item
          title="鉴定信息"
          name="3"
          :disabled="form.dead === '1'"
        >
          <van-field
            readonly
            clickable
            :value="form.fixed_date"
            name="fixed_date"
            label="定残日期"
            placeholder="请填写定残日期"
            @click="onDateClick('fixed_date')"
          />
          <van-field
            v-model="form.nutrition_duration"
            name="nutrition_duration"
            type="number"
            label="营养期限"
            placeholder="鉴定营养期限"
          >
            <template #button>
              <van-button plain size="small" type="default">天</van-button>
            </template>
          </van-field>
          <van-field
            v-model="form.delay_duration"
            name="delay_duration"
            type="number"
            label="误工期限"
            placeholder="鉴定误工期限"
          >
            <template #button>
              <van-button plain size="small" type="default">天</van-button>
            </template>
          </van-field>
          <van-field
            v-model="form.nurse_duration"
            name="nurse_duration"
            type="number"
            label="护理期限"
            placeholder="鉴定护理期限"
          >
            <template #button>
              <van-button plain size="small" type="default">天</van-button>
            </template>
          </van-field>
          <van-field
            v-model="form.nurse_persons"
            name="nurse_persons"
            type="number"
            label="护理人数"
            placeholder="鉴定护理人数"
          ></van-field>
          <van-field
            v-model="form.late_treat_fee"
            name="late_treat_fee"
            type="number"
            label="后期治疗费"
            placeholder="鉴定后期治疗费"
          >
            <template #button>
              <van-button plain size="small" type="default">元</van-button>
            </template>
          </van-field>
          <van-field name="nurse_depend_level" label="护理依赖等级">
            <template #input>
              <van-radio-group
                v-model="form.nurse_depend_level"
                direction="horizontal"
                @change="onLevelChange"
              >
                <van-radio name="50" @click="onNurseLevelClick('50')"
                  >一级</van-radio
                >
                <van-radio name="40" @click="onNurseLevelClick('40')"
                  >二级</van-radio
                >
                <van-radio name="30" @click="onNurseLevelClick('30')"
                  >三级</van-radio
                >
              </van-radio-group>
            </template>
          </van-field>
          <van-field
            v-model="form.nurse_depend_duration"
            name="nurse_depend_duration"
            type="number"
            label="护理依赖期限"
            placeholder="护理依赖期限"
          >
            <template #button>
              <van-button plain size="small" type="default">天</van-button>
            </template>
          </van-field>
          <van-field
            v-model="form.appraise_fee"
            name="appraise_fee"
            type="number"
            label="鉴定费"
            placeholder="鉴定费"
          >
            <template #button>
              <van-button plain size="small" type="default">元</van-button>
            </template>
          </van-field>
          <van-field
            readonly
            clickable
            name="top_disability"
            :value="form.top_disability"
            label="顶级伤残"
            placeholder="顶级伤残"
            @click="onDisLevelClick('top')"
          />
          <van-field
            v-for="(item, index) in form.sec_disability"
            :key="index"
            readonly
            clickable
            :name="'sec_disability' + index"
            :value="item.text"
            :label="'次级伤残[' + (index + 1) + ']'"
            placeholder="次级伤残"
            @click="onDisLevelClick('second', index)"
          >
            <template #button>
              <van-button
                type="danger"
                icon="delete"
                size="mini"
                @click.stop.prevent="onHandleDelete(index)"
              ></van-button>
            </template>
          </van-field>
          <van-row style="margin-top: 5px">
            <van-col span="8" offset="1">
              <van-button
                style="padding: 3px"
                type="primary"
                size="mini"
                @click.prevent="onHandleAdd"
                >增加次级伤残</van-button
              >
            </van-col>
          </van-row>
        </van-collapse-item>

        <!-- 被抚(扶)养人 -->
        <van-collapse-item title="被抚(扶)养人" name="4">
          <div v-for="(item, idx) in form.supports" :key="'sup' + idx">
            <van-field
              readonly
              clickable
              input-align="center"
              :name="'relation_type' + idx"
              :value="item.rel_type_name"
              :label="'当事人关系[' + (idx + 1) + ']'"
              placeholder="与当事人关系"
              @click="onRelationTypeClick('support', item)"
            >
              <template #button>
                <van-button
                  type="danger"
                  icon="delete"
                  size="mini"
                  @click.stop.prevent="onHandleDelPerson('support', idx)"
                ></van-button>
              </template>
            </van-field>
            <van-field :name="'action_ability' + idx" label="是否残障">
              <template #input>
                <van-radio-group
                  v-model="item.action_ability"
                  direction="horizontal"
                >
                  <van-radio name="1" @click="onActionClick(item, '1')"
                    >有</van-radio
                  >
                  <van-radio name="0" @click="onActionClick(item, '0')"
                    >无</van-radio
                  >
                </van-radio-group>
              </template>
            </van-field>
            <van-field :name="'is_income' + idx" label="劳动收入">
              <template #input>
                <van-radio-group
                  v-model="item.is_income"
                  direction="horizontal"
                >
                  <van-radio name="1" @click="onIncomeClick(item, '1')"
                    >有</van-radio
                  >
                  <van-radio name="0" @click="onIncomeClick(item, '0')"
                    >无</van-radio
                  >
                </van-radio-group>
              </template>
            </van-field>
            <van-field
              v-model="item.relation_age"
              :name="'relation_age' + idx"
              label="年龄"
              placeholder="请填写年龄"
            />
          </div>
          <van-row style="margin-top: 5px">
            <van-col span="8" offset="1">
              <van-button
                style="padding: 3px"
                type="primary"
                size="mini"
                @click.prevent="onHandleAddPerson('support')"
                >增加被抚(扶)养人</van-button
              >
            </van-col>
          </van-row>
        </van-collapse-item>
        <!-- 直系亲属信息 -->
        <van-collapse-item title="共同抚养人信息" name="5">
          <div v-for="(item, idx) in form.relations" :key="'rel' + idx">
            <van-field
              readonly
              clickable
              input-align="center"
              :name="'relation_type' + idx"
              :value="item.rel_type_name"
              :label="'当事人关系[' + (idx + 1) + ']'"
              placeholder="与当事人关系"
              @click="onRelationTypeClick('relation', item)"
            >
              <template #button>
                <van-button
                  type="danger"
                  icon="delete"
                  size="mini"
                  @click.stop.prevent="onHandleDelPerson('relation', idx)"
                ></van-button>
              </template>
            </van-field>
            <van-field :name="'action_ability' + idx" label="是否残障">
              <template #input>
                <van-radio-group
                  v-model="item.action_ability"
                  direction="horizontal"
                >
                  <van-radio name="1" @click="onActionClick(item, '1')"
                    >有</van-radio
                  >
                  <van-radio name="0" @click="onActionClick(item, '0')"
                    >无</van-radio
                  >
                </van-radio-group>
              </template>
            </van-field>
            <van-field :name="'is_income' + idx" label="劳动收入">
              <template #input>
                <van-radio-group
                  v-model="item.is_income"
                  direction="horizontal"
                >
                  <van-radio name="1" @click="onIncomeClick(item, '1')"
                    >有</van-radio
                  >
                  <van-radio name="0" @click="onIncomeClick(item, '0')"
                    >无</van-radio
                  >
                </van-radio-group>
              </template>
            </van-field>
            <van-field
              v-model="item.relation_age"
              :name="'relation_age' + idx"
              label="年龄"
              placeholder="请填写年龄"
            />
          </div>
          <van-row style="margin-top: 5px">
            <van-col span="8" offset="1">
              <van-button
                style="padding: 3px"
                type="primary"
                size="mini"
                @click.prevent="onHandleAddPerson('relation')"
                >增加直系亲属</van-button
              >
            </van-col>
          </van-row>
        </van-collapse-item>
      </van-collapse>
      <div style="margin: 16px">
        <van-button round block type="info">计算</van-button>
      </div>
    </van-form>
    <van-popup v-model="showPicker" round position="bottom">
      <van-picker
        title="责任认定"
        show-toolbar
        :columns="duties"
        value-key="text"
        @confirm="onDutyConfirm"
        @cancel="showPicker = false"
      />
    </van-popup>
    <!-- 伤残等级 -->
    <van-popup v-model="showLevelPicker" round position="bottom">
      <van-picker
        title="伤残等级"
        show-toolbar
        :columns="dLevels"
        value-key="text"
        @confirm="onDisLevelConfirm"
        @cancel="showLevelPicker = false"
      />
    </van-popup>
    <!-- 行业 -->
    <van-popup v-model="showIndustryPicker" round position="bottom">
      <van-picker
        title="选择行业名称"
        show-toolbar
        :columns="industries"
        value-key="name"
        @confirm="onIndustConfirm"
        @cancel="showIndustryPicker = false"
      />
    </van-popup>
    <!-- 地址 -->
    <van-popup v-model="showArea" round position="bottom" :columns-num="2">
      <van-area
        :area-list="areaList"
        value="2114"
        title="地区名称"
        @confirm="onAreaConfirm"
        @cancel="showArea = false"
      />
    </van-popup>
    <!-- 日历 -->
    <!-- <van-calendar v-model="showCalendar" @confirm="onDateConfirm" /> -->
    <van-popup v-model="showCalendar" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        title="选择时间"
        type="date"
        @confirm="onDateConfirm"
        @cancel="showCalendar = false"
      />
    </van-popup>
    <!-- 亲属关系 -->
    <van-popup v-model="showRelation" round position="bottom">
      <van-picker
        title="亲属关系"
        show-toolbar
        :columns="rTypes"
        value-key="text"
        @confirm="onRelationTypeConfirm"
        @cancel="showRelation = false"
      />
    </van-popup>
  </div>
</template>

<script>
import NavBar from "@/components/NavBar";
import {
  getIndustries,
  getProvinceList,
  getCityList,
  getIndustAverWage,
  getFuneralExpense,
  getIndustNurseWage,
  calcTraffic,
} from "@/api/calculator";
import { formatDate, dateDiff } from "@/utils/datetime";

// 默认亲属信息
const defRelation = {
  relation_type: null,
  rel_type_name: "",
  action_ability: null,
  oldAction: null,
  is_income: null,
  oldIncome: null,
  relation_age: null,
};
const defOtherIncome = {
  name: "",
  money: null,
};
export default {
  name: "Traffic",
  components: {
    NavBar,
  },
  data() {
    return {
      title: this.$route.meta.title,
      activeNames: ["1"],
      activeTab: "n1",
      form: {
        traffic_time: "",
        age: null,
        address: "",
        province_code: "",
        city_code: "",
        court_addr: "",
        court_province_code: "",
        court_city_code: "",
        dead: null,
        funeral_expense: null,
        fix_income: null,
        month_income_money: null,
        prove_income: null,
        industry: "",
        industry_id: null,
        industry_wage: null,
        other_income: [Object.assign({}, defOtherIncome)],
        spirit_fee: null,
        other_fee: null,
        asset_lost: null,
        duty_confirm_id: null,
        duty_confirm: "",
        enter_time: "",
        leave_time: "",
        hospital_day: null,
        hospital_costs: null,
        rest_day: null,
        nutrition: 0,
        leave_nutrition_fee: null,
        device_cost: null,
        car_fee: null,
        hotel_fee: null,
        cosmetic_fee: null,
        nurse_one: null,
        nurse_one_wage: null,
        nurse_one_days: null,
        nurse_two: null,
        nurse_two_wage: null,
        nurse_two_days: null,
        nurse_three: null,
        nurse_three_wage: null,
        nurse_three_days: null,
        fixed_date: "",
        nutrition_duration: "",
        delay_duration: "",
        nurse_duration: "",
        nurse_persons: "",
        late_treat_fee: null,
        nurse_depend_level: null,
        nurse_depend_duration: null,
        appraise_fee: null,
        top_disability: null,
        top_disability_id: null,
        sec_disability: [{ id: "", text: "" }],
        supports: [Object.assign({}, defRelation)],
        relations: [Object.assign({}, defRelation)],
      },
      showPicker: false,
      showIndustryPicker: false,
      showArea: false,
      showCourtArea: false,
      showCalendar: false,
      showLevelPicker: false,
      showRelation: false,
      dateFlag: "",
      areaFlag: "",
      disLevelFlag: "",
      disLevelIndex: -1, //伤残等级索引
      supportFlag: "",
      supportItem: null,
      relationItem: null,
      // 判断死亡是否改变
      oldDead: null,
      //护理
      oldNurseOne: null,
      oldNurseTwo: null,
      oldNurseThree: null,
      oldNurseLevel: null,

      duties: [
        { id: 0, text: "无责" },
        { id: 1, text: "全责" },
        { id: 2, text: "主要责任" },
        { id: 3, text: "次要责任" },
        { id: 4, text: "平等责任" },
      ],
      disLevels: [
        { id: 0, text: "空" },
        { id: 100, text: "一级" },
        { id: 90, text: "二级" },
        { id: 80, text: "三级" },
        { id: 70, text: "四级" },
        { id: 60, text: "五级" },
        { id: 50, text: "六级" },
        { id: 40, text: "七级" },
        { id: 30, text: "八级" },
        { id: 20, text: "九级" },
        { id: 10, text: "十级" },
      ],
      relTypes: [
        { id: 1, text: "父亲" },
        { id: 2, text: "母亲" },
        { id: 3, text: "子女" },
        { id: 5, text: "配偶" },
        { id: 4, text: "其他" },
        { id: 6, text: "兄弟姐妹" },
      ],
      rel_types: [
        { id: 5, text: "配偶" },
        { id: 6, text: "兄弟姐妹" },
      ],
      rTypes: [],
      dLevels: [],
      industries: [],
      areaList: {},
      currentDate: new Date(),
    };
  },
  methods: {
    getIndusList() {
      getIndustries()
        .then((res) => {
          this.industries = res.data;
        })
        .catch((error) => {
          console.log(error.message);
        });
    },
    getIndustyAverWage() {
      const ret = this.onCheck();
      if (!ret) return;
      const params = {
        province_code: this.form.province_code,
        court_province_code: this.form.court_province_code,
        indust_id: this.form.industry_id,
      };
      console.log(params);
      getIndustAverWage(params).then((res) => {
        if (res.data.success) {
          this.form.industry_wage = res.data.result.day_aver_wage;
        } else {
          this.$notify({
            type: "warning",
            message: "获取行业在岗职工平均工资失败",
          });
        }
      });
    },
    async getProvinCityList() {
      const provinces = await getProvinceList();
      const province_list = provinces.data.reduce((acc, cur) => {
        acc[cur.code] = cur.name;
        return acc;
      }, {});
      const cities = await getCityList();
      const city_list = cities.data.reduce((acc, cur) => {
        acc[cur.code] = cur.name;
        return acc;
      }, {});
      this.areaList["province_list"] = province_list;
      this.areaList["city_list"] = city_list;
    },

    onSubmit(values) {
      const openid = localStorage.getItem("openid");
      values["openid"] = openid;
      if (!values.age) {
        this.$toast("年龄不能为空");
        return false;
      }
      if (values.address == "") {
        this.$toast("家庭住址不能为空");
        return false;
      }
      if (values.court_addr == "") {
        this.$toast("法院地址不能为空");
        return false;
      }
      calcTraffic(this.form)
        .then((res) => {
          console.log(res.data);
          if (res.data.success) {
            console.log(typeof res.data.results);
            sessionStorage.setItem(
              "calc_results",
              JSON.stringify(res.data.results)
            );
            this.$router.push("/traffic_result");
          } else {
            this.$toast("计算失败");
          }
        })
        .catch((error) => {
          console.log(error.message);
          this.$toast("计算失败");
        });
    },
    onDutyConfirm(value) {
      this.form.duty_confirm = value.text;
      this.form.duty_confirm_id = value.id;
      this.showPicker = false;
    },
    onIndustConfirm(value) {
      console.log(value);
      this.form.industry = value.name;
      this.form.industry_id = value.id;
      this.showIndustryPicker = false;
      this.getIndustyAverWage();
    },
    onAreaClick(flag) {
      this.showArea = true;
      this.areaFlag = flag;
    },
    onAreaConfirm(value) {
      if (this.areaFlag == "address") {
        this.form.address = value[0].name + value[1].name;
        this.form.province_code = value[0].code;
        this.form.city_code = value[1].code;
      } else if (this.areaFlag == "court_address") {
        this.form.court_addr = value[0].name + value[1].name;
        this.form.court_province_code = value[0].code;
        this.form.court_city_code = value[1].code;
      }

      this.showArea = false;
    },
    onCheck() {
      if (!this.form.province_code) {
        this.$notify({ type: "warning", message: "请选择家庭住址" });
        this.form.nurse_one = null;
        this.form.nurse_two = null;
        this.form.nurse_three = null;
        return false;
      }
      if (!this.form.court_province_code) {
        this.$notify({ type: "warning", message: "请选择受理法院住址" });
        this.form.nurse_one = null;
        this.form.nurse_two = null;
        this.form.nurse_three = null;
        return false;
      }
      return true;
    },
    onDisLevelClick(flag, index) {
      this.showLevelPicker = true;
      this.disLevelFlag = flag;
      // 次级伤残
      this.dLevels = [];
      if (this.disLevelFlag == "second") {
        this.disLevelIndex = index;
        this.dLevels = this.disLevels.filter((item) => item.id < 100);
      } else {
        // 顶级伤残
        this.dLevels = this.disLevels;
      }
      // this.dLevels.unshift({ id: "", text: "空" });
    },
    onDisLevelConfirm(value) {
      if (this.disLevelFlag == "top") {
        this.form.top_disability = value.text;
        this.form.top_disability_id = value.id;
      } else if (this.disLevelFlag == "second") {
        console.log(this.disLevelIndex);
        this.form.sec_disability.splice(this.disLevelIndex, 1, value);
      }
      console.log(this.form);

      this.showLevelPicker = false;
    },
    onHandleAdd() {
      this.form.sec_disability.push({ id: "", text: "" });
    },
    onHandleDelete(index) {
      this.showLevelPicker = false;
      console.log(index);
      this.form.sec_disability.splice(index, 1);
    },
    // 护理依赖等级
    onLevelChange(name) {
      console.log("onLevelChange:", name);
    },
    onNurseLevelClick(name) {
      console.log(name);
      if (this.oldNurseLevel == name) {
        this.form.nurse_depend_level = null;
        this.oldNurseLevel = null;
      } else {
        this.oldNurseLevel = name;
      }
    },
    // 是否死亡
    onRadioChange(name) {
      const ret = this.onCheck();
      if (!ret) {
        this.form.dead = null;
        return;
      }

      if (name == "0" || name == null) {
        this.form.funeral_expense = null;
      } else if (name == "1") {
        const params = {
          dead: this.form.dead,
          province: this.form.province_code,
          court_province: this.form.court_province_code,
        };
        getFuneralExpense(params).then((res) => {
          if (res.data.success) {
            this.form.funeral_expense = res.data.funeral_expense;
          } else {
            this.$notify({ type: "warning", message: "丧葬费获取失败" });
          }
        });
      }
    },
    onDeadClick(name) {
      if (this.oldDead == name) {
        this.oldDead = null;
        this.form.dead = null;
      } else {
        this.oldDead = name;
      }
    },
    // 护理费
    onNurseOneChange(name) {
      const ret = this.onCheck();
      if (!ret) return;

      if (name == "1" || name == null) {
        this.form.nurse_one_wage = null;
      } else if (name == "0") {
        const params = {
          province: this.form.province_code,
          court_province: this.form.court_province_code,
        };
        getIndustNurseWage(params).then((res) => {
          if (res.data.success == "true") {
            this.form.nurse_one_wage = res.data.result.day_aver_wage;
          } else {
            this.$notify({ type: "warning", message: res.data.error_msg });
          }
        });
      }
    },
    onNurseOneClick(name) {
      if (this.oldNurseOne == name) {
        this.form.nurse_one = null;
        this.oldNurseOne = null;
      } else {
        this.oldNurseOne = name;
      }
    },
    onNurseTwoChange(name) {
      const ret = this.onCheck();
      if (!ret) return;

      if (name == "1" || name == null) {
        this.form.nurse_two_wage = null;
      } else if (name == "0") {
        const params = {
          province: this.form.province_code,
          court_province: this.form.court_province_code,
        };
        getIndustNurseWage(params).then((res) => {
          if (res.data.success == "true") {
            this.form.nurse_two_wage = res.data.result.day_aver_wage;
          } else {
            this.$notify({ type: "warning", message: res.data.error_msg });
          }
        });
      }
    },
    onNurseTwoClick(name) {
      if (this.oldNurseTwo == name) {
        this.form.nurse_two = null;
        this.oldNurseTwo = null;
      } else {
        this.oldNurseTwo = name;
      }
    },
    onNurseThreeChange(name) {
      const ret = this.onCheck();
      if (!ret) return;

      if (name == "1" || name == null) {
        this.form.nurse_three_wage = null;
      } else if (name == "0") {
        const params = {
          province: this.form.province_code,
          court_province: this.form.court_province_code,
        };
        getIndustNurseWage(params).then((res) => {
          if (res.data.success == "true") {
            this.form.nurse_three_wage = res.data.result.day_aver_wage;
          } else {
            this.$notify({ type: "warning", message: res.data.error_msg });
          }
        });
      }
    },
    //二次点击同一个选项，取消选中
    onNurseThreeClick(name) {
      if (this.oldNurseThree == name) {
        this.form.nurse_three = null;
        this.oldNurseThree = null;
      } else {
        this.oldNurseThree = name;
      }
    },
    onDateClick(flag) {
      this.showCalendar = true;
      this.dateFlag = flag;
    },
    onDateConfirm(date) {
      this.showCalendar = false;
      this.currentDate = new Date();
      if (this.dateFlag == "enter_time") {
        this.form.enter_time = formatDate(date, "YYYY-MM-DD");
      } else if (this.dateFlag == "leave_time") {
        this.form.leave_time = formatDate(date, "YYYY-MM-DD");
      } else if (this.dateFlag == "fixed_date") {
        this.form.fixed_date = formatDate(date, "YYYY-MM-DD");
        return;
      } else if (this.dateFlag == "traffic_time") {
        this.form.traffic_time = formatDate(date, "YYYY-MM-DD");
        return;
      }

      if (this.form.enter_time && this.form.leave_time) {
        this.form.hospital_day =
          dateDiff(this.form.enter_time, this.form.leave_time) - 1;
        this.form.nurse_one_days = this.form.hospital_day;
      }
    },

    onRelationTypeClick(flag, item) {
      this.supportFlag = flag;
      if (flag == "support") {
        this.rTypes = this.relTypes;
        this.supportItem = item;
      } else if (flag == "relation") {
        this.rTypes = this.rel_types;
        this.relationItem = item;
      }
      this.showRelation = true;
    },
    onRelationTypeConfirm(value) {
      if (this.supportFlag == "support") {
        this.supportItem["relation_type"] = value.id;
        this.supportItem["rel_type_name"] = value.text;
      } else if (this.supportFlag == "relation") {
        this.relationItem["relation_type"] = value.id;
        this.relationItem["rel_type_name"] = value.text;
      }
      this.showRelation = false;
    },
    onHandleAddPerson(flag) {
      if (flag == "support") {
        this.form.supports.push(Object.assign({}, defRelation));
      } else if (flag == "relation") {
        this.form.relations.push(Object.assign({}, defRelation));
      }
    },
    onHandleDelPerson(flag, idx) {
      if (flag == "support") {
        this.form.supports.splice(idx, 1);
      } else if (flag == "relation") {
        this.form.relations.splice(idx, 1);
      }
    },
    onHandleAddIncome() {
      this.form.other_income.push(Object.assign({}, defOtherIncome));
    },
    onHandleDelIncome(idx) {
      this.form.other_income.splice(idx, 1);
    },
    onIncomeClick(item, name) {
      console.log(item.is_income, name, item.oldIncome);
      if (item.is_income == item.oldIncome) {
        item.is_income = null;
        item.oldIncome = null;
      } else {
        item.oldIncome = name;
      }
    },
    onActionClick(item, name) {
      console.log(item.action_ability, name, item.oldAction);
      if (item.action_ability == item.oldAction) {
        item.oldAction = null;
        item.action_ability = null;
      } else {
        item.oldAction = name;
      }
    },
    // onRelActionClick(item, name) {
    //   console.log(item.action_ability, name, item.oldAction);
    //   if (item.action_ability == item.oldAction) {
    //     item.oldAction = null;
    //     item.action_ability = null;
    //   } else {
    //     item.oldAction = name;
    //   }
    // },
  },
  created() {
    this.getIndusList();
    this.getProvinCityList();
  },
};
</script>

<style scoped>
.title {
  font-weight: bold;
  background-color: #f5f5f5;
}
</style>
